package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.LpsOrderCdjgMapper;
import com.youlai.boot.system.service.LpsOrderCdjgService;
import com.youlai.boot.system.model.entity.LpsOrderCdjg;
import com.youlai.boot.system.model.form.LpsOrderCdjgForm;
import com.youlai.boot.system.model.query.LpsOrderCdjgQuery;
import com.youlai.boot.system.model.vo.LpsOrderCdjgVO;
import com.youlai.boot.system.converter.LpsOrderCdjgConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 触动机构服务实现类
 *
 * @author lwb
 * @since 2025-07-13 16:03
 */
@Service
@RequiredArgsConstructor
public class LpsOrderCdjgServiceImpl extends ServiceImpl<LpsOrderCdjgMapper, LpsOrderCdjg> implements LpsOrderCdjgService {

    private final LpsOrderCdjgConverter lpsOrderCdjgConverter;

    /**
    * 获取触动机构分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<LpsOrderCdjgVO>} 触动机构分页列表
    */
    @Override
    public IPage<LpsOrderCdjgVO> getLpsOrderCdjgPage(LpsOrderCdjgQuery queryParams) {
        Page<LpsOrderCdjgVO> pageVO = this.baseMapper.getLpsOrderCdjgPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取触动机构表单数据
     *
     * @param id 触动机构ID
     * @return
     */
    @Override
    public LpsOrderCdjgForm getLpsOrderCdjgFormData(Long id) {
        LpsOrderCdjg entity = this.getById(id);
        return lpsOrderCdjgConverter.toForm(entity);
    }
    
    /**
     * 新增触动机构
     *
     * @param formData 触动机构表单对象
     * @return
     */
    @Override
    public boolean saveLpsOrderCdjg(LpsOrderCdjgForm formData) {
        LpsOrderCdjg entity = lpsOrderCdjgConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新触动机构
     *
     * @param id   触动机构ID
     * @param formData 触动机构表单对象
     * @return
     */
    @Override
    public boolean updateLpsOrderCdjg(Long id,LpsOrderCdjgForm formData) {
        LpsOrderCdjg entity = lpsOrderCdjgConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除触动机构
     *
     * @param ids 触动机构ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteLpsOrderCdjgs(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的触动机构数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
